package com.brillilab.service.logic.instrument.converter;


import com.brillilab.common.exceptions.BrillilabException;
import com.brillilab.common.utils.TimeUtil;
import com.brillilab.domain.po.instrument.Instrument;
import com.brillilab.domain.po.instrument.InstrumentAppointment;
import com.brillilab.domain.po.instrument.InstrumentMaintenance;
import com.brillilab.domain.vo.instrument.AppointmentVo;
import com.brillilab.domain.vo.instrument.InstrumentTo;
import com.brillilab.domain.vo.instrument.InstrumentVo;
import com.brillilab.domain.vo.instrument.MaintenanceVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Instrument转换器
 *
 * @author wmh
 */
public class InstrumentConverter {

    private InstrumentConverter() {
    }

    /**
     * instrumentVoToInstrumentTo
     * @param instrumentVo
     * @return
     */
    public static InstrumentTo instrumentVoToInstrumentTo(InstrumentVo instrumentVo){
        InstrumentTo instrumentTo=new InstrumentTo();
        Instrument instrument=new Instrument();

        //其他属性拷贝
        BeanUtils.copyProperties(instrumentVo,instrument);
        instrumentTo.setInstrument(instrument);
        //maintenance拷贝
        if (instrumentVo.getMaintenances() != null && instrumentVo.getMaintenances().size()!=0){
            List<InstrumentMaintenance> maintenances=new ArrayList<>(instrumentVo.getMaintenances().size());
            for (MaintenanceVo e : instrumentVo.getMaintenances()) {
                InstrumentMaintenance maintenance=MaintenanceConverter.maintenanceVoToMaintenance(e);
                maintenances.add(maintenance);
            }
            instrumentTo.setMaintenances(maintenances);
        }

        //appointment拷贝
        if (instrumentVo.getAppointments() != null && instrumentVo.getAppointments().size()!=0){
            List<InstrumentAppointment> appointments=new ArrayList<>(instrumentVo.getAppointments().size());
            for (AppointmentVo e : instrumentVo.getAppointments()) {
                InstrumentAppointment appointment=AppointmentConverter.appointmentVoToAppointment(e);
                appointments.add(appointment);
            }
            instrumentTo.setAppointments(appointments);
        }

        //labmemberIds
        instrumentTo.setLabMemberIds(instrumentVo.getLabMemberIds());

        return instrumentTo;
    }

    /**
     * instrumentToToinstrumentVo
     * @param instrumentTo
     * @return
     */
    public static InstrumentVo instrumentToInstrumentVo(InstrumentTo instrumentTo) {

        InstrumentVo instrumentVo=new InstrumentVo();

        Instrument instrument = instrumentTo.getInstrument();
        List<InstrumentMaintenance> maintenances = instrumentTo.getMaintenances();
        List<InstrumentAppointment> appointments = instrumentTo.getAppointments();

        //其他属性拷贝
        BeanUtils.copyProperties(instrument,instrumentVo);

        //maintenance拷贝
        if (!CollectionUtils.isEmpty(maintenances)){
            List<MaintenanceVo> maintenanceVos=new ArrayList<>(maintenances.size());
            maintenances.forEach(e->{
                MaintenanceVo maintenanceVo = MaintenanceConverter.maintenanceTomaintenanceVo(e);
                maintenanceVos.add(maintenanceVo);
            });
            instrumentVo.setMaintenances(maintenanceVos);
        }

        //appointment拷贝
        if (!CollectionUtils.isEmpty(appointments)){
            List<AppointmentVo> appointmentVos=new ArrayList<>(appointments.size());
            appointments.forEach(e->{
                AppointmentVo appointmentVo = AppointmentConverter.appointmentToAppointmentVo(e);
                appointmentVos.add(appointmentVo);
            });
            instrumentVo.setAppointments(appointmentVos);
        }

        //设置仪器状态（维护状态在这里设置）
        if(instrument.getState()==0){
            instrumentVo.setState(0);

        }else {
            boolean flag=isMaintenace(instrumentVo);
            if(flag)
                instrumentVo.setState(2);
            else
                instrumentVo.setState(1);
        }

        return instrumentVo;
    }

    /**
     * instrumentToInstrumentVoBack （回显信息）
     * @param instrumentTo
     * @return
     */
    public static InstrumentVo instrumentToInstrumentVoBack(InstrumentTo instrumentTo){

        InstrumentVo instrumentVo=new InstrumentVo();

        Instrument instrument = instrumentTo.getInstrument();
        List<InstrumentMaintenance> maintenances = instrumentTo.getMaintenances();
        List<InstrumentAppointment> appointments = instrumentTo.getAppointments();

        //其他属性拷贝
        BeanUtils.copyProperties(instrument,instrumentVo);

        //maintenance拷贝
        if (!CollectionUtils.isEmpty(maintenances)){
            List<MaintenanceVo> maintenanceVos=new ArrayList<>(maintenances.size());
            maintenances.forEach(e->{
                MaintenanceVo maintenanceVo = MaintenanceConverter.maintenanceTomaintenanceVo(e);
                maintenanceVos.add(maintenanceVo);
            });
            instrumentVo.setMaintenances(maintenanceVos);
        }

        //appointment拷贝
        if (!CollectionUtils.isEmpty(appointments)){
            List<AppointmentVo> appointmentVos=new ArrayList<>(appointments.size());
            appointments.forEach(e->{
                AppointmentVo appointmentVo = AppointmentConverter.appointmentToAppointmentVo(e);
                appointmentVos.add(appointmentVo);
            });
            instrumentVo.setAppointments(appointmentVos);
        }

        return instrumentVo;
    }

    /**
     * 判断是否为维护状态
     * @param instrumentVo
     * @return
     */
    private static boolean isMaintenace(InstrumentVo instrumentVo){
        Calendar calendar = Calendar.getInstance();
        Date now=new Date();
        calendar.setTime(now);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH)+1;
        int date = calendar.get(Calendar.DATE);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK)-1;

        boolean flag=false;
        if (!CollectionUtils.isEmpty(instrumentVo.getMaintenances())){
            int i=0;
            for (MaintenanceVo e:instrumentVo.getMaintenances()){
                //每周维护
                if (e.getType()==0){
                    if(dayOfWeek > e.getStartDay() && dayOfWeek < e.getEndDay()){
                        flag=true;
                    }

                    if(e.getStartDay().equals(e.getEndDay()) && e.getStartDay().equals(dayOfWeek)){
                        flag=isMaintenance(year,month,date,e);
                    }else {
                        if(dayOfWeek == e.getStartDay()){
                            Date starttime=TimeUtil.getDirectDate(year,month,date,e.getStartTime());
                            if(starttime.getTime()<=now.getTime()){
                                flag=true;
                            }
                        }

                        if(dayOfWeek == e.getEndDay()){
                            Date endTime=TimeUtil.getDirectDate(year,month,date,e.getEndTime());
                            if(endTime.getTime()>=now.getTime()){
                                flag=true;
                            }
                        }
                    }
                }

                //每月维护
                if (e.getType()==1){
//                    if(date >= e.getStartday() && date <= e.getEndday())
//                        flag=isMaintenance(year,month,date,e);
                    if(date > e.getStartDay() && date < e.getEndDay()){
                        flag=true;
                    }

                    if(e.getStartDay().equals(e.getEndDay()) && e.getStartDay()==date){
                        flag=isMaintenance(year,month,date,e);
                    }else {
                        if(date == e.getStartDay()){
                            Date starttime=TimeUtil.getDirectDate(year,month,date,e.getStartTime());
                            if(starttime.getTime()<=now.getTime()){
                                flag=true;
                            }
                        }
                        if(date == e.getEndDay()){
                            Date endTime=TimeUtil.getDirectDate(year,month,date,e.getEndTime());
                            if(endTime.getTime()>=now.getTime()){
                                flag=true;
                            }
                        }
                    }
                }

                //每年维护
                if(e.getType()==2){

                    if(month > e.getStartMonth() && month < e.getEndMonth()){
                        flag=true;
                    }

                    if(e.getStartMonth().equals(e.getEndMonth()) && e.getStartDay().equals(e.getEndDay()) && e.getStartDay()==date){
                        flag=isMaintenance(year,month,date,e);
                    }else {
                        if(month == e.getStartMonth()){
                            if(date>e.getStartDay()){
                                flag=true;
                            }
                            if(date==e.getStartDay()){
                                Date starttime=TimeUtil.getDirectDate(year,e.getStartMonth(),e.getStartDay(),e.getStartTime());
                                if(starttime.getTime()<=now.getTime()){
                                    flag=true;
                                }
                            }
                        }

                        if(month == e.getEndMonth()){
                            if(date < e.getEndDay()){
                                flag=true;
                            }
                            if(date == e.getEndDay()){
                                Date endtime=TimeUtil.getDirectDate(year,e.getEndMonth(),e.getEndDay(),e.getEndTime());
                                if(endtime.getTime()>=now.getTime()){
                                    flag=true;
                                }
                            }
                        }
                    }

                }
            }
        }
        return flag;
    }

    private static boolean isMaintenance(int year,int month,int date,MaintenanceVo e){
        Date starttime=TimeUtil.getDirectDate(year,month,date,e.getStartTime());
        Date endTime=TimeUtil.getDirectDate(year,month,date,e.getEndTime());
        return TimeUtil.isCoincidence(new Date(),starttime,endTime);
    }

    /**
     * 批量转换
     * @param instrumentTos
     * @return
     */
    public static List<InstrumentVo> instrumentToListInstrumentVoList(List<InstrumentTo> instrumentTos){

        ArrayList<InstrumentVo> instrumentVos=new ArrayList<>();
        for (InstrumentTo e:instrumentTos){
            InstrumentVo instrumentVo=InstrumentConverter.instrumentToInstrumentVo(e);
            instrumentVo.setLabMembers(e.getLabMembers());
            instrumentVo.setIsAll(e.getIsAll());
            instrumentVo.setUsable(e.getUsable());
            instrumentVos.add(instrumentVo);
        }
        return instrumentVos;
    }
}
